//Documentation complete
/**
 * @author Kazu
 * @version 2009MR24
 */

package problemDomain;

import static problemDomain.ErrorBean.throwError;
import static problemDomain.ErrorBean.ErrorCode.*;
import static utilities.XMLTools.encodeEntities;

import java.util.Calendar;

/**
 * A payment on an order.
 */
public class PaymentBean implements Bean {
	
	/* Constants */
	
	private static final long serialVersionUID = 4237059431774643979L;
	
	/* END Constants */
	
	
	/* Attributes */
	
	private int id;
	private OrderBean order;
	private String paymentMethod;
	private String status;
	private Calendar datePaid;
	private double amount;
	
	/* END Attributes */
	
	
	/* Constructors */
	
	/**
	 * Constructs a <code>PaymentBean</code> with default values.
	 */
	public PaymentBean () {
		id = 0;
		paymentMethod = "";
		status = "";
		datePaid = null;
		amount = 0.0;
		order = null;
    }
	
	/**
	 * Constructs a <code>PaymentBean</code> with an ID.
	 * @param id the ID of the payment
	 */
	public PaymentBean (int id) {
		this();
		this.id = id;
    }
	
	/**
	 * Constructs a <code>PaymentBean</code> with specified values.
	 * @param paymentID The ID number of the payment
	 * @param order the order being paid
	 * @param paymentMethod The method of the payment
	 * @param paymentStatus The status of the payment
	 * @param datePaid The date when the payment is processed
	 * @param amount The amount of the payment
	 */
	public PaymentBean (int paymentID, OrderBean order, String paymentMethod,
			String paymentStatus, Calendar datePaid, double amount) {
		this.amount = amount;	
		this.datePaid = datePaid;
		this.paymentMethod = paymentMethod;
		this.id = paymentID;
		this.order = order;
		this.status = paymentStatus;
	}

	/* END Constructors */
	
	
	/* Accessors */

	/**
	 * Returns the ID number of the payment.
	 * @return the ID number of the payment
	 */
	public int getId () {
		return id;
	}
	
	/**
	 * Returns the order this payment is for.
	 * @return the order this payment is for
	 */
	public OrderBean getOrder () {
		return order;
	}

	/**
	 * Returns the type of the payment.
	 * @return the type of the payment
	 */
	public String getPaymentMethod () {
		return paymentMethod;
	}
	
	/**
	 * Returns the status of the payment.
	 * @return the status of the payment.
	 */
	public String getStatus () {
		return status;
	}
	
	/**
	 * Returns the date when the payment is processed.
	 * @return the date when the payment is processed
	 */
	public Calendar getDatePaid () {
		return datePaid;
	}
	
	/**
	 * Returns the amount of the payment.
	 * @return the amount of the payment
	 */
	public double getAmount () {
		return amount;
	}
	
	/* END Accessors */
	
	
	/* Mutators */
	
	/**
	 * Sets the ID number of the payment.
	 * @param paymentID the ID number of the payment to set
	 */
	public void setId (int paymentID) {
		this.id = paymentID;
	}
	
	/**
	 * Sets the order that this payment is for.
	 * @param order the order this payment is for
	 */
	public void setOrder (OrderBean order) {
		this.order = order;
	}

	/**
	 * Sets the method of the payment.
	 * @param paymentMethod the method of the payment
	 */
	public void setPaymentMethod (String paymentMethod) {
		this.paymentMethod = paymentMethod;
	}

	/**
	 * Sets the status of the payment
	 * @param paymentStatus the paymentStatus to set
	 */
	public void setStatus (String paymentStatus) {
		this.status = paymentStatus;
	}
	
	/**
	 * Sets the paid date.
	 * @param datePaid the date paid
	 */
	public void setDatePaid (Calendar datePaid) {
		this.datePaid = datePaid;
	}

	/**
	 * Sets the amount.
	 * @param amount the amount to set
	 */
	public void setAmount (double amount) {
		this.amount = amount;
	}
	
	/* END Mutators */
	
	
	/* (Bean) Methods */

	@Override
	public boolean isValid () throws ErrorBean {
		if (amount < 0)
			throwError(
				PAYMENT_AMOUNT_INVALID,
				String.valueOf(amount),
				this);
		
		if (datePaid == null)
			throwError(
				PAYMENT_DATE_NULL,
				this);
		
		if (id < 0)
			throwError(
				PAYMENT_ID_INVALID,
				String.valueOf(id),
				this);
		
		if (order != null)
			order.isValid();
		
		if (paymentMethod == null)
			throwError(
				PAYMENT_PAYMENT_METHOD_INVALID,
				this);
		
		return true;
	}

	@Override
	public String toXML() {
		StringBuilder sb = new StringBuilder();
		sb.append("<payment>");
		sb.append("<id>" + id + "</id>");
		if (order != null)
			sb.append("<order>" + order.toXML() + "</order>");
		else
			sb.append("<order></order>");
		sb.append("<method>" + (paymentMethod != null ? paymentMethod : "") + "</method>");
		sb.append("<status>" + (status != null ? status : "") + "</status>");
		sb.append("<datepaid>" + (datePaid != null ? datePaid : "") + "</datepaid>");
		sb.append("<amount>" + amount + "</amount>");
		sb.append("</payment>");
		return encodeEntities(sb.toString());
	}
	
	/* END (Bean) Methods */
	
	
	/* (Object) Method */

	@Override
	public String toString () {
		StringBuilder sb = new StringBuilder();
		sb.append("ID: " + id + "\n");
		sb.append("Order:\n");
		if (order != null)
			sb.append(order.toString().replaceAll("^(.*)$", "\t$1") + '\n');
		else
			sb.append("\tnull\n");
		sb.append("Pay Method: " + paymentMethod + "\n");
		sb.append("Payment Status: " + status + "\n");
		sb.append("Date Paid: " + datePaid + "\n");
		sb.append("Amount: " + amount + "\n\n");
		return sb.toString();
	}
	
	/* END (Object) Method */
	
}
